#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+7;
int n, m;
int e[N], ne[N*2], h[N], idx;
bool vis[N];
void add(int a, int b) {
  e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
typedef pair<int,int> pi;
queue<pi> q;

int bfs() {
  q.push({1, 0});
  while(q.size()) {
    auto [u, d] = q.front(); q.pop();
    if(u==n) {
      return d;
    }
    vis[u] = true;
    for(int i=h[u]; i!=-1; i=ne[i]) {
      int v = e[i];
      if(!vis[v]) {
        q.push({v, d+1});
      }
    }
  }
  return -1;
}
int main() {
  memset(h, -1, sizeof(h));
  cin>>n>>m;
  while(m--) {
    int u, v;
    cin>>u>>v;
    add(u, v);/* add(v, u); */
  }
  cout<<bfs()<<endl;
}
